// Copyright: 北京经海科技有限公司
// Author: Ivan (李渊)
// Date: 2015-03-01
// Last Modified On: 2015-05-22
// Description: 辅助类，用于给图像增加标注

#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include "element.h"

using namespace std;



double Element::m_scale = 1.0; /* 单位微米 */

Element::Element()
    : m_drawType(NONE),
      m_state(DS_IDLE)
{
	m_textFont = QFont("宋体", 12, QFont::Bold);
    m_linePen = QPen( QColor(255, 0, 0) );
	m_linePen.setWidth(2);
	m_isPicked = false;

	m_box = QRect(0, 0, 32768, 32768);
}

DrawLCorner::DrawLCorner(void)
    : m_line1_start(0, 0),
      m_line1_end(0, 0),
      m_line2_start(0, 0),
      m_line2_end(0, 0)
{
    m_drawType = LINECORNER;
    m_state = Element::DS_IDLE;
}
DrawLCorner::~DrawLCorner( )
{
}


int DrawLCorner::mouseMove(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        m_line1_end = point;
        return 1;
    }
    else if( m_state == Element::DS_STATE3 )
    {
        m_line2_end = point;
        return 1;
    }
    return 0;
}

int DrawLCorner::mouseLButtonPress(QPoint point)
{
    if( m_state == Element::DS_IDLE )
    {
        m_state = Element::DS_STATE1;
        m_line1_start = point;
        m_line1_end = point;
        return 1;
    }
    if( m_state == Element::DS_STATE2 )
    {
        m_state = Element::DS_STATE3;
        m_line2_start = point;
        m_line2_end = point;
        return 1;
    }
    return 0;
}

int DrawLCorner::mouseLButtonRelease(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        m_line1_end = point;
        if( m_line1_start == m_line1_end )
        {
            m_state = Element::DS_ERROR;
        }
        else
        {
            m_state = Element::DS_STATE2;
        }
        return 1;
    }
    else if(m_state == Element::DS_STATE3)
    {
        m_line2_end = point;
        if( m_line2_start == m_line2_end )
        {
            m_state = Element::DS_ERROR;
        }
        else
        {
            m_state = Element::DS_FINISH;
        }
        return 1;
    }
    return 0;
}

void DrawLCorner::computeBox(void)
{
}

inline void DrawLCorner::computeLength(void)
{
    m_length = hypot( m_line1_end.x() - m_line1_start.x(), m_line1_end.y() - m_line1_start.y() );
    m_length += hypot( m_line2_end.x() - m_line2_start.x(), m_line2_end.y() - m_line2_start.y() );
}

void DrawLCorner::computeAngle()
{

}

void DrawLCorner::updateText(void)
{
    m_text = QString("L = %1 um").arg(m_length * m_scale, 2, 'f', 2);
}

void DrawLCorner::draw( QPainter &painter )
{
    if( m_state != Element::DS_IDLE )
    {
        painter.save();
        painter.setPen(m_linePen);

        painter.drawLine ( m_line1_start, m_line1_end );
        if(m_state >= Element::DS_STATE3)
        {
            painter.drawLine ( m_line2_start, m_line2_end );
        }
        if(m_state == Element::DS_FINISH)
        {
            painter.setFont(m_textFont);
            painter.drawText((m_line1_start + m_line1_end) / 2, m_text);
        }
        painter.restore();
    }
}
